package ltd.hyangyang.vegetable.controller;

import ltd.hyangyang.vegetable.entity.ClassificationEntity;
import ltd.hyangyang.vegetable.service.ClassificationService;
import ltd.hyangyang.vegetable.service.TrainService;
import ltd.hyangyang.vegetable.utils.*;
import ltd.hyangyang.vegetable.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;

@RestController
@RequestMapping("/train")
public class TrainController {

    @Autowired
    private TrainService trainService;

    @Autowired
    private ClassificationService classificationService;

    @Autowired
    private WebSocketServer webSocketServer;

    @Autowired
    private TrainProperties trainProperties;

    @RequestMapping("/uploadFolder")
    public Result uploadFolder( FolderUploadParams folderUploadParams){

        String trainPath = trainProperties.getTrainPicturePath()+"Training";
        String testPath = trainProperties.getTrainPicturePath()+"Test";
        //将图片保存到本地
        trainService.uploadFolder(trainPath,folderUploadParams);
        trainService.uploadFolder(testPath,folderUploadParams);
       return Result.success();
    }

    //添加分类
    @RequestMapping("addClass")
    public Result add(@RequestBody ClassificationEntity classificationEntity){
        trainService.add(classificationEntity);
        return Result.success();
    }

    //训练模型
    @RequestMapping("/toTrain")
    public Result toTrain(){
        String[] args = {"python",trainProperties.getScriptPath(),trainProperties.getTrainPicturePath(),trainProperties.getModelSavePath()};
        String out= ScriptUtils.runScript(args);
        //读取文件内容

        webSocketServer.sendMessageToAll(out);
        return Result.success();
    }

//    @RequestMapping("/test")
//    public Result test(){
//        webSocketServer.sendMessageToAll("你好，");
//        return Result.success();
//    }
}
